package com.marry.multidatasource.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * 不动产配置
 *
 * @author ml
 * @create 2018-01-04--14:59
 */
@Configuration
@MapperScan(basePackages = "com.marry.multidatasource.mapper.bd",sqlSessionTemplateRef = "bdSqlSessionTemplate")
public class BDDataSourceConfig {

    @Bean(name = "bdDataSource",initMethod = "init",destroyMethod = "close")
    @ConfigurationProperties(prefix = "spring.datasource.druid.bd")
    @Primary
    public DruidDataSource bdDataSource(){
        return new DruidDataSource();
    }


    @Bean(name = "bdSqlSessionFactory")
    @Primary
    public SqlSessionFactory bdSqlSessionFactory(@Qualifier("bdDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean=new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/bd/*.xml"));
        factoryBean.setTypeAliasesPackage("com.marry.multidatasource.model.bd");
        factoryBean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis/bd-mybatis-config.xml"));
        return factoryBean.getObject();
    }

    @Bean(name = "bdTransactionManager")
    @Primary
    public DataSourceTransactionManager bdTransactionManager(@Qualifier("bdDataSource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "bdSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate bdSqlSessionTemplate(@Qualifier("bdSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    @Bean
    public ServletRegistrationBean statViewServlet(){

        ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        servletRegistrationBean.addInitParameter("loginUsername","malin");
        servletRegistrationBean.addInitParameter("loginPassword","123456");

        servletRegistrationBean.addInitParameter("resetEnable","true");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean statFilter(){
        FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(new WebStatFilter());

        filterRegistrationBean.addUrlPatterns("/*");

        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;

    }
}
